package cn.loioi.web.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.loioi.multi.common.BaseService;
import cn.loioi.util.RequestUtil;
import cn.loioi.util.page.JqGrid;
import cn.loioi.web.components.shiro.common.Permission;

/**
 * 通用控制器-JqGrid
 * @文件 :BaseJqGridController.java
 * @作者 :cn.loioi
 * @创建时间 :2016年10月13日 下午1:10:43
 */
public class BaseJqGridController<T, S extends BaseService<T>> extends BaseUserController {
	protected S baseService;
	/** 添加接口是否可用 */
	protected boolean add = true;
	/** 编辑接口是否可用 */
	protected boolean edit = true;
	/** 删除接口是否可用 */
	protected boolean del = true;

	public BaseJqGridController(String baseTemplateUrl, String identity, S baseService) {
		super(baseTemplateUrl, identity);
		this.baseService = baseService;
	}

	public BaseJqGridController(String baseTemplateUrl, S baseService) {
		super(baseTemplateUrl);
		this.baseService = baseService;
	}

	/**
	 * 列表页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("index.jspx")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 检查用户的权限是否可以访问
		checkPermission(_identity);
		// 执行正常操作
		model.addAttribute("page", RequestUtil.getValueStr(request, "page"));
		return returnUrl("index", model);
	}

	/**
	 * 获取数据接口
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list.json")
	public Model list(HttpServletRequest request, Model model) {
		try {
			// 检查用户的权限是否可以访问
			checkPermission(_identity);
			// 执行正常操作
			PageInfo<T> page = getData(request, new JqGrid<>(request));
			if (page.getList() != null && page.getList().size() > 0) {
				model.addAttribute("success", true);
				model.addAttribute("page", page.getPageNum());
				model.addAttribute("total", page.getPages());
				model.addAttribute("rows", page.getList());
				return renderSuccess(null, model, "加载成功");
			} else {
				return renderFail(model, "没有数据");
			}
		} catch (AuthorizationException e) {
			return renderFail(model, "你有没权限访问该接口");
		}
	}

	/**
	 * 添加数据接口
	 */
	@ResponseBody
	@RequestMapping(value = "save.json", params = "oper=add")
	public Model add(HttpServletRequest request, HttpServletResponse response, Model model, T record, BindingResult result) {
		if (!add) return renderFail(model, "该接口未开放");
		try {
			checkPermission(Permission.ADD.parts(_identity));
			return addHandle(request, model, record);
		} catch (AuthorizationException e) {
			return renderFail(model, "你有没权限访问该接口");
		}
	}

	/**
	 * 修改数据接口
	 */
	@ResponseBody
	@RequestMapping(value = "save.json", params = "oper=edit")
	public Model edit(HttpServletRequest request, HttpServletResponse response, Model model, T record, BindingResult result) {
		if (!edit) return renderFail(model, "该接口未开放");
		try {
			checkPermission(Permission.UPDATE.parts(_identity));
			return editHandle(request, model, record);
		} catch (AuthorizationException e) {
			return renderFail(model, "你有没权限访问该接口");
		}
	}

	/**
	 * 删除数据接口
	 */
	@ResponseBody
	@RequestMapping(value = "save.json", params = "oper=del")
	public Model del(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam(name = "id", required = true, defaultValue = "") String id) {
		if (!del) return renderFail(model, "该接口未开放");
		try {
			checkPermission(Permission.DELETE.parts(_identity));
			return delHandle(request, model, id);
		} catch (AuthorizationException e) {
			return renderFail(model, "你有没权限访问该接口");
		}
	}

	/**
	 * 异常操作
	 */
	@ResponseBody
	@RequestMapping(value = "save.json")
	public Model other(Model model) {
		return renderFail(model, "没有有效的请求类型");
	}

	/**
	 * 添加一条记录
	 * @param request 请求
	 * @param model 返回
	 * @param record 数据
	 * @return
	 */
	protected Model addHandle(HttpServletRequest request, Model model, T record) {
		if (baseService.insert(record) > 0) {
			return renderSuccess(null, model, "添加成功");
		} else {
			return renderFail(model, "添加失败");
		}
	}

	/**
	 * 更新一条记录
	 * @param request 请求
	 * @param model 返回
	 * @param record 数据
	 * @return
	 */

	protected Model editHandle(HttpServletRequest request, Model model, T record) {
		if (baseService.update(record) > 0) {
			return renderSuccess(null, model, "更新成功");
		} else {
			return renderFail(model, "更新失败");
		}
	}

	/**
	 * 删除
	 * @param request 请求
	 * @param model 返回
	 * @param id
	 * @return
	 */
	protected Model delHandle(HttpServletRequest request, Model model, String id) {
		if (baseService.delete(id) > 0) {
			return renderSuccess(null, model, "成功删除");
		} else {
			return renderFail(model, "删除失败");
		}
	}

	/**
	 * 查询数据库
	 * @param request
	 * @param jqGrid
	 * @return
	 */
	protected PageInfo<T> getData(HttpServletRequest request, JqGrid<T> jqGrid) {
		return new PageInfo<>(baseService.selectByJqGrid(jqGrid));
	}

	/**
	 * 检查权限及保存页码
	 * @param model
	 * @param request
	 * @param identity
	 */
	protected void checkPermissionAndSavePage(Model model, HttpServletRequest request, String... identity) {
		checkPermission(identity);
		model.addAttribute("page", RequestUtil.getValueStr(request, "page"));
	}

	/**
	 * 设置save接口下的3个通用接口是否可用
	 * @param add 添加接口
	 * @param edit 编辑接口
	 * @param del 删除接口
	 */
	protected final void setSaveUse(boolean add, boolean edit, boolean del) {
		this.add = add;
		this.edit = edit;
		this.del = del;
	}
}
